#include "scene_logic_handle.h"
static LIGHT_EFFECT_T light_effect_dazzling_temporary_config;
void aurora_mode_handle();  //极光
void candlelight_mode_handle();  //烛光
void birthday_mode_handle();  //生日
void meteor_mode_handle();  //5流星
void rest_mode_handle();  //休息
void meditation_mode_handle();  //冥想
void flame_mode_handle();  //火焰
void business_mode_handle();  //商务
void alarm_mode_handle();  //警报
void tide_mode_handle();  //潮汐
void music_mode_handle();  //电子音乐
void universe_mode_handle();  //宇宙

void scene_sente_recover(u8 senet) //恢复灵敏度
{
    u8 scene_senet; //灵敏度
    if(driver_nvs_part_read_u8(scene_senst_save[get_scene_ssp()],&scene_senet) == ESP_OK)
    {
        set_scene_senet(scene_senet);
    }
    else
    {
        set_scene_senet(senet);
    }
}

SCENE_HANDLE scene_mode_dazzling_handle[SCENE_MODE_NUM] =
{
    {}, //0号
    {aurora_mode_handle}, //1极光模式   
    {candlelight_mode_handle}, //2烛光
    {birthday_mode_handle},//3生日
    {meteor_mode_handle}, //4流星
    {rest_mode_handle},//5休息
    {meditation_mode_handle},//6冥想
    {flame_mode_handle},//7火焰
    {business_mode_handle},//8商务
    {alarm_mode_handle},//9警报
    {tide_mode_handle},//10潮汐
    {music_mode_handle},//11电子音乐
    {universe_mode_handle},//12宇宙
};

void scene_mode_handle()
{
    if(scene_mode_dazzling_handle[get_scene_ssp()].handle)
    {
        scene_mode_dazzling_handle[get_scene_ssp()].handle();
    }
}



void scene_logic_handle(SYS_OPT_KEY opt)
{
    static int time;
    if (app_logic_get_last_ssp() != app_logic_get_ssp()) //第一次进入
    {
        LOG_INFO("scene_logic_handle\n");
        set_custom_recssp(0); //强制复位
        app_logic_set_last_ssp(app_logic_get_ssp());
        time = arch_os_ms_now();
        disp_clear_cw_light(); //消除CW亮度
    }

    if(arch_os_ms_elapsed(time)>=(10)) //ms轮询一次
    {
        scene_mode_handle();
    }
    
    if (opt.oepation == OPT_POWER_TURNOFF)
    {
        app_logic_set_ssp(SSP_POWER_OFF);
    }
    else if (opt.oepation == OPT_PREE_TR)
    {
        if((get_scene_ssp()+1)<SCENE_MODE_NUM)
        {
            set_scene_ssp(get_scene_ssp()+1); //下一个
        }
        else
        {
            set_scene_ssp(SCENE_MODE_NUM-1);
        }
    }
    else if (opt.oepation == OPT_PREE_TL)
    {
        if((get_scene_ssp()-1)>SCENE_ZERO)
        {
            set_scene_ssp(get_scene_ssp()-1); //下一个
        }
        else
        {
            set_scene_ssp(SCENE_ZERO+1);
        }
    }
    else if(opt.oepation == OPT_GO_NEXT) //进入麦克风模式
    {
        app_logic_set_ssp(SSP_MODE_MIC);
    }

}

void aurora_mode_handle()  //1 极光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void candlelight_mode_handle()  //2 烛光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}


void birthday_mode_handle()  //3 生日
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void meteor_mode_handle()  //4 流星
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}


void rest_mode_handle()  //5 休息
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        printf("light_effect_dazzling_temporary_config.wait is %d\n",light_effect_dazzling_temporary_config.wait);
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void meditation_mode_handle()  //6 冥想
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void flame_mode_handle()  //7 火焰
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}


void business_mode_handle()  //8 商务
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void alarm_mode_handle()  //9 警报
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void tide_mode_handle()  //10 潮汐
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}

void music_mode_handle()  //11 电子音乐
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}



void universe_mode_handle()  //12 宇宙
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[get_scene_ssp()],sizeof(LIGHT_EFFECT_T));
        scene_sente_recover(light_effect_dazzling_temporary_config.wait);
        light_effect_dazzling_temporary_config.wait = get_scene_senet();
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    set_light_wait(get_scene_senet());
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());
}